"use strict";

const path = require("path");
const fs = require("fs");

// 上传配置
module.exports = {
    // 获取上传目录（不再接受客户端自定义路径）
    getUploadDir() {
        // 使用环境变量或默认值
        // 可以设置 IMAGE_UPLOAD_DIR 环境变量来改变存储路径
        // 改为直接使用 uploads/images 目录
        const baseDir = process.env.IMAGE_UPLOAD_DIR || 'uploads/images';

        // 按日期组织目录结构
        const now = new Date();
        const year = now.getFullYear();
        const month = String(now.getMonth() + 1).padStart(2, '0');
        const day = String(now.getDate()).padStart(2, '0');

        return path.join(baseDir, `${year}/${month}/${day}`);
    },

    // 确保目录存在
    ensureDir(dirPath) {
        const fullPath = path.join(__dirname, '..', dirPath);
        if (!fs.existsSync(fullPath)) {
            fs.mkdirSync(fullPath, { recursive: true });
        }
        return fullPath;
    },

    // 生成文件名
    generateFileName(originalName) {
        const ext = path.extname(originalName);
        const baseName = path.basename(originalName, ext);
        const timestamp = Date.now();
        const random = Math.round(Math.random() * 1E9);
        return `${baseName}-${timestamp}-${random}${ext}`;
    },

    // 文件大小限制
    limits: {
        fileSize: parseInt(process.env.MAX_FILE_SIZE || 10 * 1024 * 1024), // 默认10MB
    },

    // 允许的文件类型
    allowedMimeTypes: [
        'image/jpeg',
        'image/jpg',
        'image/png',
        'image/gif',
        'image/webp',
        'image/svg+xml'
    ]
};